package com.csmzxy.soft.comm;

import java.util.ArrayList;
import java.util.List;


/*
 * @accor dasd
 * */
public class Account {
	private String id;
	private String password;
	protected double balance;
	private Customer customer;
	private List <Transaction> listTrans;
	
	public Account(){
		this("123456","123456",10000);
		
	}
	public Account(String id,String password,double balance){
		this("123456","123456",10000,null);
	}
	public Account(String id,String password,double balance,Customer customer){
		this.balance=balance;
		this.id=id;
		this.password=password;
		this.setCustomer(customer);
		if(customer!=null){
			customer.addAccount(this);
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
		record(0.0,"修改密码");
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}
	
	public void deposit(double money){
		this.balance+=money;
		record(money,"存款");
	}
	public boolean withdraw(double money){
		if(this.balance>=money){
			this.balance-=money;
			record(money,"取款");
			return true;
		}else{
			return false;
		}
	}
	public String toString(){
		return "[账号"+id+",密码："+password+",余额："+balance+"]";
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public List <Transaction> getListTrans() {
		return listTrans;
	}
	public void setListTrans(List <Transaction> listTrans) {
		this.listTrans = listTrans;
	}
	public boolean verify(String id,String pwd){
		if(this.id.compareTo(id)==0&&this.password.compareTo(pwd)==0){
			return true;
		}else{
			return false;
		}
	}
	public boolean transfer(Account acc,double money){
		if(balance>=money){
			balance-=money;
			acc.deposit(money);
			record(money,"转出"+acc.getId()+"]");
			return true;
		}else{
			return false;
		}
	}
	private void record(double amount,String operation){
		Transaction trans=new Transaction(amount,operation);
		if(listTrans==null)
			listTrans=new ArrayList<Transaction>();
		listTrans.add(trans);
	}
	public String getTransRecord(){
		String msg="";
		if(listTrans==null){
			return "暂无记录";
		}
		for(Transaction trans:listTrans){
			msg+=trans.toString()+"\r\n";
		}
		return msg;
	}
}
